<template>
  <div class="shop-list">
    <el-form ref="shopListForm"
             :model="shopListForm"
             :rules="shopListFormRules"
             :inline="true"
             class="shopListForm">
      <el-form-item label="店铺编号/名称"
                    label-width="140px"
                    prop="shopname">
        <el-input v-model="shopListForm.shopname"></el-input>
      </el-form-item>
      <el-form-item label="行业"
                    prop="industry">
        <el-select v-model="shopListForm.industry"
                   placeholder="请选择行业"
                   label-width="80px">
          <el-option v-for="item in allindustry"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="shopListFormBtn">
        <el-button type="primary"
                   @click="queryShopList">查询</el-button>
        <el-button @click="resetForm">取消</el-button>
      </el-form-item>
    </el-form>
    <div class="shopCountInfo">
      <div class="shopCount">
        店铺数量:
        <span>{{shopNumTotal}}</span>
      </div>
      <div class="shareShopCount">
        共享店铺数量:
        <span>{{translateShopNum}}</span>
      </div>
      <div class="saleTotal">
        销售额:
        <span>{{saleMoneyTotal}}</span>
      </div>
    </div>
    <div class="shopListTable">
      <el-table :data="shopList">
        <!-- <el-table-column
          type="index"
        >
        </el-table-column> -->
        <el-table-column prop="shop_id"
                         label="店铺编号"
                         width="80">
        </el-table-column>
        <el-table-column prop="shop_name"
                         label="店铺名称"
                         width="120">
        </el-table-column>
        <el-table-column prop="industry"
                         label="店铺行业"
                         width="80">
        </el-table-column>
        <el-table-column prop="shop_address"
                         label="店铺地址"
                         width="200">
        </el-table-column>
        <el-table-column prop="tel_phone"
                         label="联系电话"
                         width="120">
        </el-table-column>
        <el-table-column prop="shopPromoter"
                         label="推广人ID"
                         width="100">
        </el-table-column>
        <el-table-column prop="interestLevel"
                         label="权益级别"
                         width="100">
        </el-table-column>
        <el-table-column prop="interestTime"
                         label="权益时限"
                         width="100">
        </el-table-column>
        <el-table-column prop="saleMoney"
                         label="销售额"
                         width="110">
        </el-table-column>
        <el-table-column prop="operateStatus"
                         label="运营状态"
                         width="80">
        </el-table-column>
        <el-table-column prop="shopStatus"
                         label="状态"
                         width="60">
          <template slot-scope="scope">
            <el-tag type="success"
                    v-if="scope.row.shopStatus">启用</el-tag>
            <el-tag type="danger"
                    v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="shop_remark"
                         label="备注">
        </el-table-column>
        <el-table-column label="操作"
                         width="300">
          <template slot-scope="scope">
            <span class="handleBtn"
                  @click="goDetail(scope.$index, storageKey)">详情</span>
            <span class="handleBtn"
                  @click="$router.push({ name: 'GoodsList', params: { shopName: scope.row.shop_name } })">查看商品</span>
            <span class="handleBtn"
                  @click="$router.push({ name:'goods-analyze', params: shopList[scope.$index] })">经营分析</span>
            <span class="handleBtn"
                  @click="onShopStatus(scope.$index)">{{shopList[scope.$index].shopStatus ? '禁用' : '启用'}}</span>
            <span class="handleBtn"
                  @click="deleteGoods(scope.$index)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getShopList } from '@/api/test.js'
import { getItem, setItem } from '@/utils/storage'
export default {
  name: 'ShopList',
  data () {
    return {
      shopListForm: {
        shopname: '',
        industry: ''
      },
      shopList: [],
      shopNumTotal: 1,
      translateShopNum: 0,
      saleMoneyTotal: 0,
      storageKey: 'SHOPLIST',
      allindustry: [],
      shopListFormRules: {}
    }
  },
  props: {},
  components: {},
  created () {
    this.loadTest()
  },
  mounted () {
  },
  computed: {},
  methods: {
    async loadTest () {
      if (getItem(this.storageKey)) {
        const shopList = getItem(this.storageKey)
        this.shopList = shopList
        this.countMoney(shopList)
        // this.allindustry = shopList[0].allindustry
        return
      }
      const { data: res } = await getShopList();
      console.log(res.data);
      this.shopList = res.data
      this.countMoney(res.data)
      setItem(this.storageKey, res.data)
      console.log(this.shopList);
      console.log(this.allindustry);
    },
    countMoney (arr) {
      this.saleMoneyTotal = 0
      this.translateShopNum = 0
      arr.forEach(item => {
        this.saleMoneyTotal += item.saleMoney
        if (item.operateStatus === '共享经营') {
          this.translateShopNum++
        }
      })
      this.allindustry = this.shopList[0].allindustry
      this.shopNumTotal = this.shopList.length
    },
    goDetail (id, key) {
      console.log(id);
      this.$router.push({
        name: 'shop-detail',
        params: {
          id,
          key
        }
      })
    },
    deleteGoods (id) {
      this.shopList.splice(id, 1)
      setItem(this.storageKey, this.shopList)
    },
    resetForm () {
      this.$refs.shopListForm.resetFields()
      this.loadTest()
    },
    queryShopList () {
      if (this.shopListForm.industry === '' && this.shopListForm.shopname === '') return
      const queryResults = this.shopList.filter(item => {
        if (this.shopListForm.industry) {
          return (this.shopListForm.shopname === item.shop_name || this.shopListForm.shopname - 0 === item.shop_id) && this.shopListForm.industry === item.industry
        } else {
          return this.shopListForm.shopname === item.shop_name || this.shopListForm.shopname - 0 === item.shop_id
        }
      })
      this.shopList = queryResults
      this.countMoney(this.shopList)
    },
    onShopStatus (index) {
      this.shopList[index].shopStatus = !this.shopList[index].shopStatus
    }
  }
}
</script>
<style scoped lang="less">
.handleBtn {
  cursor: pointer;
}
.shop-list {
  padding: 15px;
}
.shopListForm {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding-top: 15px;
  .shopListFormBtn {
    margin-left: 30px;
  }
}
.shopCountInfo {
  display: flex;
  height: 50px;
  line-height: 50px;
  .shopCount {
    margin-left: 15px;
  }
  .shopCount,
  .shareShopCount {
    margin-right: 50px;
  }
}
/deep/.shopListTable {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 10px;
  width: 100%;
  tbody {
    td {
      text-align: center;
    }
  }
  thead {
    th {
      text-align: center;
    }
  }
  .handleBtn {
    text-decoration: underline;
    color: blue;
    margin-right: 5px;
    &:last-child {
      color: red;
    }
  }
}
</style>